package com.Artificialintelligence.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.Artificialintelligence.common.core.domain.model.LoginBody;
import com.Artificialintelligence.common.utils.StringUtils;
import com.Artificialintelligence.common.utils.PageUtils;
import com.Artificialintelligence.common.core.page.PagePlus;
import com.Artificialintelligence.common.core.page.TableDataInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.Artificialintelligence.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.Artificialintelligence.system.domain.bo.SysAuthUserBo;
import com.Artificialintelligence.system.domain.vo.SysAuthUserVo;
import com.Artificialintelligence.system.domain.SysAuthUser;
import com.Artificialintelligence.system.mapper.SysAuthUserMapper;
import com.Artificialintelligence.system.service.ISysAuthUserService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 第三方授权Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-14
 */
@Service
public class SysAuthUserServiceImpl extends ServicePlusImpl<SysAuthUserMapper, SysAuthUser, SysAuthUserVo> implements ISysAuthUserService {

    @Autowired
    private SysAuthUserMapper sysAuthUserMapper;

    @Override
    public SysAuthUserVo queryById(Long authId){
        return getVoById(authId);
    }

    @Override
    public TableDataInfo<SysAuthUserVo> queryPageList(SysAuthUserBo bo) {
        PagePlus<SysAuthUser, SysAuthUserVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysAuthUserVo> queryList(SysAuthUserBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<SysAuthUser> buildQueryWrapper(SysAuthUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysAuthUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUuid()), SysAuthUser::getUuid, bo.getUuid());
        lqw.eq(bo.getUserId() != null, SysAuthUser::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getLoginName()), SysAuthUser::getLoginName, bo.getLoginName());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), SysAuthUser::getUserName, bo.getUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatar()), SysAuthUser::getAvatar, bo.getAvatar());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), SysAuthUser::getEmail, bo.getEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getSource()), SysAuthUser::getSource, bo.getSource());
        return lqw;
    }

    @Override
    public Boolean insertByBo(SysAuthUserBo bo) {
        SysAuthUser add = BeanUtil.toBean(bo, SysAuthUser.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setAuthId(add.getAuthId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(SysAuthUserBo bo) {
        SysAuthUser update = BeanUtil.toBean(bo, SysAuthUser.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 批量更改密码
     * */
    @Override
    public void updateAuthUser(SysAuthUserBo bo){
        sysAuthUserMapper.updateAuthUser(bo);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysAuthUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    /**
     * 查询用户的第三方信息
     * */
    public SysAuthUser selelerauthusre(LoginBody loginBody){
        SysAuthUser sysAuthUser = new SysAuthUser();
        sysAuthUser.setUuid(loginBody.getUuid());
        sysAuthUser.setSource(loginBody.getSource());
        return sysAuthUserMapper.selelerauthusre(sysAuthUser);
    }
}
